<?php
require_once __DIR__ . '/../core/config.php';

// 发布帖子函数
function post_forum($title, $content, $img, $author, $classify, $label) {
    $pdo = connect_db();
    $redis = connect_redis();
    $redis_key = "forum-author:$author";

    if ($redis->get($redis_key)) {
        return json_response(429, '发布频繁 请30秒后重试');
    }
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, username, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$author]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '用户不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法发布');
        }
        $current_time = date('Y-m-d H:i:s');
        $forum_condition = ($user_info['user_permissions'] == 0) ? '0' : '1';
        $stmt = $pdo->prepare("INSERT INTO star_forum (title, content, img, author, issue_time, modify_time, forum_condition) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$title, $content, $img, $user_info['username'], $current_time, $current_time, $forum_condition]);
        $forum_uid = $pdo->lastInsertId();
        if (!empty($classify)) {
            $classify_list = explode('-', $classify);
            foreach ($classify_list as $classify_name) {
                $stmt = $pdo->prepare("SELECT uid FROM star_forum_classify WHERE name = ?");
                $stmt->execute([$classify_name]);
                $classify_info = $stmt->fetch(PDO::FETCH_ASSOC);
                if (!$classify_info) {
                    return json_response(500, "$classify_name-分类不存在");
                }
                $stmt = $pdo->prepare("INSERT INTO star_forum_classify_list (forum_uid, classify_uid) VALUES (?, ?)");
                $stmt->execute([$forum_uid, $classify_info['uid']]);
            }
        }
        if (!empty($label)) {
            $label_list = explode('-', $label);
            foreach ($label_list as $label_name) {
                $stmt = $pdo->prepare("SELECT uid FROM star_forum_label WHERE name = ?");
                $stmt->execute([$label_name]);
                $label_info = $stmt->fetch(PDO::FETCH_ASSOC);
                if (!$label_info) {
                    return json_response(500, "$label_name-标签不存在");
                }
                $stmt = $pdo->prepare("INSERT INTO star_forum_label_list (forum_uid, label_uid) VALUES (?, ?)");
                $stmt->execute([$forum_uid, $label_info['uid']]);
            }
        }
        $redis->setex($redis_key, 30, 1);
        return json_response(200, '帖子发布成功');
    } catch (PDOException $e) {
        $redis->setex($redis_key, 30, 1);
        return json_response(500, '发布失败: ' . $e->getMessage());
    }
}


// 修改帖子的函数
function change_forum($uid, $author, $title, $content, $img, $issue_time, $classify, $label) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, username, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$author]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '用户不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法修改帖子');
        }
        $stmt = $pdo->prepare("SELECT author, user_permissions FROM star_forum f JOIN star_user u ON f.author = u.username WHERE f.uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        if ($forum_info['author'] !== $user_info['username'] && $user_info['user_permissions'] != 0) {
            return json_response(500, '您没有权限修改帖子');
        }
        $current_time = date('Y-m-d H:i:s');
        $update_fields = [];
        $params = [];
        if (!empty($title)) {
            $update_fields[] = 'title = ?';
            $params[] = $title;
        }
        if (!empty($content)) {
            $update_fields[] = 'content = ?';
            $params[] = $content;
        }
        if (!empty($img)) {
            $update_fields[] = 'img = ?';
            $params[] = $img;
        }
        if (!empty($issue_time)) {
            $update_fields[] = 'issue_time = ?';
            $params[] = $issue_time;
        }
        $update_fields[] = 'modify_time = ?';
        $params[] = $current_time;
        $update_sql = "UPDATE star_forum SET " . implode(', ', $update_fields) . " WHERE uid = ?";
        $params[] = $uid;
        $stmt = $pdo->prepare($update_sql);
        $stmt->execute($params);
        if (!empty($classify)) {
            $stmt = $pdo->prepare("DELETE FROM star_forum_classify_list WHERE forum_uid = ?");
            $stmt->execute([$uid]);

            $classify_list = explode('-', $classify);
            foreach ($classify_list as $classify_name) {
                $stmt = $pdo->prepare("SELECT uid FROM star_forum_classify WHERE name = ?");
                $stmt->execute([$classify_name]);
                $classify_info = $stmt->fetch(PDO::FETCH_ASSOC);

                if (!$classify_info) {
                    return json_response(500, "$classify_name-分类不存在");
                }
                $stmt = $pdo->prepare("INSERT INTO star_forum_classify_list (forum_uid, classify_uid) VALUES (?, ?)");
                $stmt->execute([$uid, $classify_info['uid']]);
            }
        }
        if (!empty($label)) {
            $stmt = $pdo->prepare("DELETE FROM star_forum_label_list WHERE forum_uid = ?");
            $stmt->execute([$uid]);

            $label_list = explode('-', $label);
            foreach ($label_list as $label_name) {
                $stmt = $pdo->prepare("SELECT uid FROM star_forum_label WHERE name = ?");
                $stmt->execute([$label_name]);
                $label_info = $stmt->fetch(PDO::FETCH_ASSOC);

                if (!$label_info) {
                    return json_response(500, "$label_name-标签不存在");
                }
                $stmt = $pdo->prepare("INSERT INTO star_forum_label_list (forum_uid, label_uid) VALUES (?, ?)");
                $stmt->execute([$uid, $label_info['uid']]);
            }
        }
        return json_response(200, '帖子修改成功');
    } catch (PDOException $e) {
        return json_response(500, '修改失败: ' . $e->getMessage());
    }
}

// 删除帖子的函数
function delete_forum($uid, $author) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, username, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$author]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '用户不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        $stmt = $pdo->prepare("SELECT author, user_permissions FROM star_forum f JOIN star_user u ON f.author = u.username WHERE f.uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        if ($forum_info['author'] !== $user_info['username'] && $user_info['user_permissions'] == 1) {
            return json_response(500, '请使用发布者账号删除帖子');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁，无法删除');
        }
        $stmt = $pdo->prepare("DELETE FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $stmt = $pdo->prepare("DELETE FROM star_forum_classify_list WHERE forum_uid = ?");
        $stmt->execute([$uid]);
        $stmt = $pdo->prepare("DELETE FROM star_forum_label_list WHERE forum_uid = ?");
        $stmt->execute([$uid]);
        $stmt = $pdo->prepare("DELETE FROM star_forum_comment WHERE forum_uid = ?");
        $stmt->execute([$uid]);
        $stmt = $pdo->prepare("DELETE FROM star_forum_praise WHERE praise_uid = ?");
        $stmt->execute([$uid]);
        $stmt = $pdo->prepare("DELETE FROM star_forum_visit WHERE forum_uid = ?");
        $stmt->execute([$uid]);

        return json_response(200, '帖子删除成功');
    } catch (PDOException $e) {
        return json_response(500, '删除失败: ' . $e->getMessage());
    }
}

//获取帖子列表的函数
function forum_list() {
    $pdo = connect_db();
    try {
        $stmt = $pdo->query("SELECT uid, title, content, img, author, issue_time, modify_time, praise, visit FROM star_forum");
        $forums = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (empty($forums)) {
            return json_response(200, '没有帖子发布', []);
        }
        foreach ($forums as &$forum) {
            $stmt = $pdo->prepare("SELECT c.name FROM star_forum_classify_list cl JOIN star_forum_classify c ON cl.classify_uid = c.uid WHERE cl.forum_uid = ?");
            $stmt->execute([$forum['uid']]);
            $forum['classifies'] = $stmt->fetchAll(PDO::FETCH_COLUMN);
            $stmt = $pdo->prepare("SELECT l.name FROM star_forum_label_list ll JOIN star_forum_label l ON ll.label_uid = l.uid WHERE ll.forum_uid = ?");
            $stmt->execute([$forum['uid']]);
            $forum['labels'] = $stmt->fetchAll(PDO::FETCH_COLUMN);
        }
        return json_response(200, '帖子列表获取成功', $forums);
    } catch (PDOException $e) {
        return json_response(500, '获取帖子列表失败: ' . $e->getMessage());
    }
}

// 获取指定用户帖子的函数
function forum_list_user($account) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法查看帖子');
        }
        $stmt = $pdo->prepare("SELECT uid, title, content, img, author, issue_time, modify_time, praise, visit FROM star_forum WHERE author = ?");
        $stmt->execute([$account]);
        $forums = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (empty($forums)) {
            return json_response(200, '没有帖子发布', []);
        }
        foreach ($forums as &$forum) {
            $stmt = $pdo->prepare("SELECT c.name FROM star_forum_classify_list cl JOIN star_forum_classify c ON cl.classify_uid = c.uid WHERE cl.forum_uid = ?");
            $stmt->execute([$forum['uid']]);
            $forum['classifies'] = $stmt->fetchAll(PDO::FETCH_COLUMN);
            $stmt = $pdo->prepare("SELECT l.name FROM star_forum_label_list ll JOIN star_forum_label l ON ll.label_uid = l.uid WHERE ll.forum_uid = ?");
            $stmt->execute([$forum['uid']]);
            $forum['labels'] = $stmt->fetchAll(PDO::FETCH_COLUMN);
        }
        return json_response(200, '帖子获取成功', $forums);
    } catch (PDOException $e) {
        return json_response(500, '获取帖子列表失败: ' . $e->getMessage());
    }
}


// 帖子点赞统计的函数
function like_statistics($uid, $account) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法点赞');
        }
        $stmt = $pdo->prepare("SELECT praise FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        $stmt = $pdo->prepare("SELECT * FROM star_forum_praise WHERE user_uid = ? AND praise_uid = ?");
        $stmt->execute([$account, $uid]);
        $praise_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($praise_info) {
            return json_response(500, '已点赞 请勿重复点赞');
        }
        $new_praise_count = (int)$forum_info['praise'] + 1;
        $stmt = $pdo->prepare("UPDATE star_forum SET praise = ? WHERE uid = ?");
        $stmt->execute([$new_praise_count, $uid]);
        $stmt = $pdo->prepare("INSERT INTO star_forum_praise (praise_uid, user_uid) VALUES (?, ?)");
        $stmt->execute([$uid, $account]);
        return json_response(200, '点赞成功');
    } catch (PDOException $e) {
        return json_response(500, '点赞失败: ' . $e->getMessage());
    }
}

// 取消帖子点赞的函数
function cancel_praise($uid, $account) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已封禁 无法取消点赞');
        }
        $stmt = $pdo->prepare("SELECT praise FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        $stmt = $pdo->prepare("SELECT * FROM star_forum_praise WHERE praise_uid = ? AND user_uid = ?");
        $stmt->execute([$uid, $account]);
        $praise_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$praise_info) {
            return json_response(500, '您尚未点赞该帖子');
        }
        $stmt = $pdo->prepare("DELETE FROM star_forum_praise WHERE praise_uid = ? AND user_uid = ?");
        $stmt->execute([$uid, $account]);
        $new_praise_count = max(0, (int)$forum_info['praise'] - 1);
        $stmt = $pdo->prepare("UPDATE star_forum SET praise = ? WHERE uid = ?");
        $stmt->execute([$new_praise_count, $uid]);
        return json_response(200, '取消点赞成功');
    } catch (PDOException $e) {
        return json_response(500, '取消点赞失败: ' . $e->getMessage());
    }
}

// 文章评论的函数
function add_comment($uid, $account, $content) {
    $pdo = connect_db();
    $redis = connect_redis();
    $redis_key = "comment-account:$account";
    if ($redis->get($redis_key)) {
        return json_response(429, '评论过于频繁 请15秒后重试');
    }
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法评论');
        }
        $stmt = $pdo->prepare("SELECT uid FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        $current_time = date('Y-m-d H:i:s');
        $stmt = $pdo->prepare("INSERT INTO star_forum_comment (forum_uid, account, comment, time) VALUES (?, ?, ?, ?)");
        $stmt->execute([$uid, $account, $content, $current_time]);
        $redis->setex($redis_key, 15, 1);

        return json_response(200, '评论成功');
    } catch (PDOException $e) {
        $redis->setex($redis_key, 15, 1);
        return json_response(500, '评论失败: ' . $e->getMessage());
    }
}

// 评论删除的函数
function delete_comment($uid, $account, $forum_uid) {
    $pdo = connect_db();
    $redis = connect_redis();
    $redis_key = "delete-comment-account:$account";
    if ($redis->get($redis_key)) {
        return json_response(429, '操作过于频繁 请15秒后重试');
    }
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法删除评论');
        }
        $stmt = $pdo->prepare("SELECT * FROM star_forum_comment WHERE uid = ? AND forum_uid = ?");
        $stmt->execute([$uid, $forum_uid]);
        $comment_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$comment_info) {
            return json_response(500, '评论不存在');
        }
        if ($comment_info['account'] !== $account && $user_info['user_permissions'] != 0) {
            return json_response(500, '您没有权限删除这条评论');
        }
        $stmt = $pdo->prepare("DELETE FROM star_forum_comment WHERE uid = ?");
        $stmt->execute([$uid]);
        $redis->setex($redis_key, 15, 1);
        return json_response(200, '评论删除成功');
    } catch (PDOException $e) {
        $redis->setex($redis_key, 15, 1);
        return json_response(500, '删除评论失败: ' . $e->getMessage());
    }
}

// 修改评论函数
function edit_comment($uid, $account, $forum_uid, $new_comment) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁，无法修改评论');
        }
        $stmt = $pdo->prepare("SELECT * FROM star_forum_comment WHERE uid = ? AND forum_uid = ?");
        $stmt->execute([$uid, $forum_uid]);
        $comment_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$comment_info) {
            return json_response(500, '评论不存在');
        }
        if ($comment_info['account'] !== $account && $user_info['user_permissions'] != 0) {
            return json_response(500, '您没有权限修改这条评论');
        }
        $stmt = $pdo->prepare("UPDATE star_forum_comment SET comment = ? WHERE uid = ?");
        $stmt->execute([$new_comment, $uid]);
        return json_response(200, '评论修改成功');
    } catch (PDOException $e) {
        return json_response(500, '修改评论失败: ' . $e->getMessage());
    }
}

// 帖子浏览量添加的函数
function record_visit($uid, $account) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        $stmt = $pdo->prepare("SELECT visit FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        $stmt = $pdo->prepare("SELECT * FROM star_forum_visit WHERE forum_uid = ? AND user_uid = ?");
        $stmt->execute([$uid, $account]);
        $visit_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($visit_info) {
            return;
        }
        $current_time = date('Y-m-d H:i:s');
        $stmt = $pdo->prepare("INSERT INTO star_forum_visit (forum_uid, user_uid, time) VALUES (?, ?, ?)");
        $stmt->execute([$uid, $account, $current_time]);
        $new_visit_count = (int)$forum_info['visit'] + 1;
        $stmt = $pdo->prepare("UPDATE star_forum SET visit = ? WHERE uid = ?");
        $stmt->execute([$new_visit_count, $uid]);
        return;
    } catch (PDOException $e) {
        return json_response(500, '访问记录失败: ' . $e->getMessage());
    }
}

// 获取帖子评论的函数
function comment_list($uid) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT uid FROM star_forum WHERE uid = ?");
        $stmt->execute([$uid]);
        $forum_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$forum_info) {
            return json_response(500, '帖子不存在');
        }
        $stmt = $pdo->prepare(" SELECT c.uid AS comment_uid, c.comment, c.time, u.username AS account FROM star_forum_comment c JOIN star_user u ON c.account = u.account WHERE c.forum_uid = ? ORDER BY c.time DESC");
        $stmt->execute([$uid]);
        $comments = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return json_response(200, '评论列表获取成功', $comments);
    } catch (PDOException $e) {
        return json_response(500, '获取评论列表失败: ' . $e->getMessage());
    }
}

// 获取指定用户评论的函数
function editor_user_comment($account) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁 无法查看评论');
        }
        $stmt = $pdo->prepare("
            SELECT 
                c.uid AS comment_uid, 
                c.forum_uid, 
                c.comment, 
                c.time, 
                f.title AS forum_title
            FROM 
                star_forum_comment c
            JOIN 
                star_forum f ON c.forum_uid = f.uid
            WHERE 
                c.account = ?
            ORDER BY 
                c.time DESC
        ");
        $stmt->execute([$account]);
        $comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return json_response(200, '评论列表获取成功', $comments);
    } catch (PDOException $e) {
        return json_response(500, '获取评论列表失败: ' . $e->getMessage());
    }
}

//获取分类下帖子的函数
function classify_list($classify_uid) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT uid, name, introduce, head_portrait, account, time FROM star_forum_classify WHERE uid = ?");
        $stmt->execute([$classify_uid]);
        $classify_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$classify_info) {
            return json_response(500, '分类不存在');
        }
        if ($classify_info['forum_condition'] == 1) {
            return json_response(500, '分类审核中');
        } elseif ($classify_info['forum_condition'] == 2) {
            return json_response(500, '该分类已被封禁');
        }
        $stmt = $pdo->prepare("
            SELECT 
                f.uid, 
                f.title, 
                f.content, 
                f.img, 
                f.author, 
                f.issue_time, 
                f.modify_time, 
                f.praise, 
                f.visit
            FROM 
                star_forum f
            JOIN 
                star_forum_classify_list cl ON f.uid = cl.forum_uid
            WHERE 
                cl.classify_uid = ?
        ");
        $stmt->execute([$classify_uid]);
        $forums = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (empty($forums)) {
            return json_response(200, '没有帖子发布', []);
        }
        return json_response(200, '帖子列表获取成功', [
            'classify_info' => $classify_info,
            'forums' => $forums
        ]);
    } catch (PDOException $e) {
        return json_response(500, '获取帖子列表失败: ' . $e->getMessage());
    }
}

//获取所有分类的函数
function get_classify() {
    $pdo = connect_db();
    try {
        $stmt = $pdo->query("
            SELECT DISTINCT 
                uid, 
                name, 
                introduce, 
                head_portrait, 
                account, 
                time
            FROM 
                star_forum_classify
        ");
        $classifies = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if (empty($classifies)) {
            return json_response_1(200, '没有分类信息'); // 返回空数据时，不包含 data
        }

        // 将分类数据直接传递给 json_response_1
        return json_response_1(200, '分类获取成功', $classifies);
    } catch (PDOException $e) {
        return json_response_1(500, '获取分类信息失败: ' . $e->getMessage());
    }
}

//添加分类函数
function add_classify($account, $name, $introduce, $head_portrait) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁，无法操作');
        }
        $current_time = date('Y-m-d H:i:s');
        $stmt = $pdo->prepare("
            INSERT INTO star_forum_classify (name, introduce, head_portrait, account, time, forum_condition)
            VALUES (?, ?, ?, ?, ?, ?)
        ");
        $forum_condition = ($user_info['user_permissions'] == 1) ? 1 : 0;
        $stmt->execute([$name, $introduce, $head_portrait, $account, $current_time, $forum_condition]);

        if ($user_info['user_permissions'] == 1) {
            return json_response(200, '申请成功，请等待审核');
        } else {
            return json_response(200, '分类添加成功');
        }
    } catch (PDOException $e) {
        return json_response(500, '添加分类失败: ' . $e->getMessage());
    }
}

//删除分类函数
function delete_classify($account, $classify_uid) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = ?");
        $stmt->execute([$account]);
        $user_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user_info) {
            return json_response(500, '账号不存在');
        }
        $current_time = time();
        $token_time = strtotime($user_info['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($user_info['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁，无法操作');
        }
        $stmt = $pdo->prepare("SELECT uid, account FROM star_forum_classify WHERE uid = ?");
        $stmt->execute([$classify_uid]);
        $classify_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$classify_info) {
            return json_response(500, '分类不存在');
        }
        if ($classify_info['account'] !== $account && $user_info['user_permissions'] != 0) {
            return json_response(500, '非管理员和分类发布者无法删除分类');
        }
        $stmt = $pdo->prepare("DELETE FROM star_forum_classify WHERE uid = ?");
        $stmt->execute([$classify_uid]);
        return json_response(200, '分类删除成功');
    } catch (PDOException $e) {
        return json_response(500, '删除分类失败: ' . $e->getMessage());
    }
}